home
***
CD-ROM
|
disk
|
FTP
|
other
***
search
/
Disc to the Future 2
/
Disc to the Future Part II Programmer's Reference (Wayzata Technology)(6013)(1992).bin
/
MAC
/
MACSHELL
/
MS1
/
SHELL_SO
/
SHELL.C
< prev
next >
Wrap
Text File
|
1992-12-02
|
10KB
|
411 lines
/*
* MacShell Source File
*
* Copyright (c) 1989, 1990, 1991, 1992 Suick Bay Technologies. All rights reserved.
*
*
* RESTRICTIONS ON MacShell program and source code.
*
* Ñ╩MacShell¬ is a product of Suick Bay Technologies and is provided for
* restricted use by the owner of the CDROM "Disk to the future II".
*
* Ñ╩No permission is granted for any commercial use without the written
* consent of the Suick Bay Technologies.
*
* Ñ╩No permission is granted for any redistribution of any kind use without
* the written consent of the Suick Bay Technologies.
*
* Ñ╩Permission is granted to use this for any personal noncommercial use.
*
* Ñ╩You may not distribute source or executable code at all, nor may you
* distribute it with or within a commercial product without the written
* consent of the Suick Bay Technologies. Please send modifications to
* the author for inclusion in updates to the program. Thanks.
*
*
* MacShell¬ IS PROVIDED AS IS WITH NO WARRANTIES OF ANY KIND INCLUDING THE
* WARRANTIES OF DESIGN, MERCHANTIBILITY AND FITNESS FOR A PARTICULAR
* PURPOSE, OR ARISING FROM A COURSE OF DEALING, USAGE OR TRADE PRACTICE.
*
* SUICK BAY TECHNOLOGIES SHALL HAVE NO LIABILITY WITH RESPECT TO THE
* INFRINGEMENT OF COPYRIGHTS, TRADE SECRETS OR ANY PATENTS BY MACSHELL
* OR ANY PART THEREOF.
*
* In no event will Suick Bay Technologies be liable for any lost revenue
* or profits or other special, indirect and consequential damages, even if
* Suick Bay Technologies has been advised of the possibility of such damages.
*
* Suick Bay Technologies can be reached at:
*
* 8768 Cottonwood lane
* Maple Grove, MN 55369
* Voice: (612) 425-7025
* AppleLink: D5233
*
*
* No parts of this software may be reproduced or stored in a
* retrieval system or transmitted in any form, or any means,
* electronic, mechanical, photocopying, recording or otherwise,
* without the prior written permission of Suick Bay Technologies.
*
* Spread the word and not the disk.
*
* SPK 030690 : cleaned up add shell string
* SPK 030590 : Added command history
* SPK 012290 : Initial
*/
#include <WindowMgr.h>
#include "System.h"
#include "Global.h"
#include "Struct.h"
#include "Shell.h"
#include "Path.h"
#include "Mac.h"
#include "Prefs.h"
/*******************************************************************
* Shell command history FUNCTIONS
*******************************************************************/
void RecordCmd( char *string, WHandle ShellWh )
{
char str[ 32 ], *cp, save;
ShellWindRec **MyShell = (ShellWindRec **) (**ShellWh).thing;
ShellSetVar( ShellWh, "COMMAND", string );
/* save new command */
(**MyShell).lastCmdNum++;
sprintf( str, "%d_CMD", (**MyShell).lastCmdNum );
/* remove and crs */
save = '\0';
cp = string;
while( *cp )
{
if( *cp == '\r'|| *cp == '\n' )
{
save = *cp;
*cp = '\0';
break;
}
cp++;
}
ShellSetVar( ShellWh, str, string );
if( save == '\r'|| save == '\n' )
*cp = save;
/* reset current command */
(**MyShell).currCmdNum = (**MyShell).lastCmdNum;
/* first ever ? */
if( (**MyShell).firstCmdNum == 0 )
(**MyShell).firstCmdNum = (**MyShell).currCmdNum;
/* To many commands saved in history ? */
if( ((**MyShell).lastCmdNum - (**MyShell).firstCmdNum) > MAXCMDHISTORY )
{
sprintf( str, "%d_CMD", (**MyShell).firstCmdNum );
ShellDelVar( ShellWh, str );
(**MyShell).firstCmdNum++;
}
}
void ResetCmdInput( char *string, WHandle ShellWh )
{
ShellWindRec **MyShell = (ShellWindRec **) (**ShellWh).thing;
PEHandle hPE;
int32 len;
hPE = (**MyShell).ihPE;
len = (int32) strlen( string );
PESetSelect( (**MyShell).promptBase, (**hPE).peLength, hPE );
PEDelete( hPE );
PEInsert( string, len, hPE );
PESetSelect( (**hPE).peLength, (**hPE).peLength, hPE );
}
void LastCmd( WHandle ShellWh )
{
char str[ 32 ];
ShellWindRec **MyShell = (ShellWindRec **) (**ShellWh).thing;
/* is their a command available */
if( (**MyShell).lastCmdNum > 0 )
{
sprintf( str, "%d_CMD", (**MyShell).currCmdNum );
ResetCmdInput( ShellGetVar( ShellWh, str ), ShellWh );
(**MyShell).currCmdNum--;
if( (**MyShell).currCmdNum < (**MyShell).firstCmdNum )
(**MyShell).currCmdNum = (**MyShell).lastCmdNum;
}
}
void NextCmd( WHandle ShellWh )
{
char str[ 32 ];
ShellWindRec **MyShell = (ShellWindRec **) (**ShellWh).thing;
/* is their a command available */
if( (**MyShell).lastCmdNum > 0 )
{
sprintf( str, "%d_CMD", (**MyShell).currCmdNum );
ResetCmdInput( ShellGetVar( ShellWh, str ), ShellWh );
(**MyShell).currCmdNum++;
if( (**MyShell).currCmdNum > (**MyShell).lastCmdNum )
(**MyShell).currCmdNum = (**MyShell).firstCmdNum;
}
}
/*******************************************************************/
void ClearShellOutput( WHandle ShellWh )
{
PEHandle hPE;
ShellWindRec **MyShell;
MyShell = (ShellWindRec **) ((**ShellWh).thing);
hPE = (**MyShell).hPE;
PESetSelect( 0, 200000L, hPE );
PEDelete( hPE );
SetPeditEditStatus( hPE, ShellWh );
SetUndo( hPE );
}
/*******************************************************************/
char promptStr[ 64 ];
char *GetPromptStr( WHandle ShellWh )
{
char *cp;
ShellWindRec **MyPedit;
*promptStr = '\0';
if( (**ShellWh).WindAppType == ShellWindowID )
{
MyPedit = (ShellWindRec **) ((**ShellWh).thing);
if( (**MyPedit).prompt )
cp = ShellGetVar( ShellWh, "PS2" );
else
cp = ShellGetVar( ShellWh, "PS1" );
strcpy( promptStr, cp );
ExpandShellStr( ShellWh, promptStr );
}
return( promptStr );
}
void DoShellPrompt( WHandle ShellWh )
{
ShellWindRec **MyPedit;
char *cp;
int32 ss, se, pb, base = 1;
if( (**ShellWh).WindAppType == ShellWindowID )
{
MyPedit = (ShellWindRec **) ((**ShellWh).thing);
cp = GetPromptStr( ShellWh );
(**MyPedit).promptBase = strlen( cp );
ss = (**(**MyPedit).ihPE).selStart;
se = (**(**MyPedit).ihPE).selEnd;
pb = (**MyPedit).promptBase;
PESetSelect( 0L, 0L, (**MyPedit).ihPE );
PEInsert( cp, pb, (**MyPedit).ihPE );
PESetSelect( ss+pb, se+pb, (**MyPedit).ihPE );
}
}
/*******************************************************************/
void CleanShellInput( WHandle ShellWh )
{
ShellWindRec **MyPedit;
char *cp;
int32 ss, se, pb, base = 1;
if( (**ShellWh).WindAppType == ShellWindowID )
{
MyPedit = (ShellWindRec **) ((**ShellWh).thing);
PESetSelect( 0L, (**(**MyPedit).ihPE).selEnd, (**MyPedit).ihPE );
PEDelete( (**MyPedit).ihPE );
}
}
/*******************************************************************/
WHandle currShell = NULL; /* Window Handler Handle */
void SetShellName( WHandle ShellWh, char *name )
{
char buf[ 256 ];
if( name )
strcpy( buf, name );
else
GetCurrPath( buf );
CtoPstr( buf );
SetWTitle( (**ShellWh).whWind, buf );
}
/*******************************************************************/
static int numShells = 1;
void ShellWindInit( char *windowName )
{
WHandle ShellWh = NULL; /* Window Handler Handle */
ShellWindRec **MyShell;
int16 i;
int32 len = sizeof( ShellWindRec );
int32 free;
free = FreeMem();
/*
* Check for sufficient memeory
*/
if( free < (sizeof( ShellWindRec ) + 8192L ) )
{
printf( "Not enough memory\n" );
SysBeep( 1 );
return;
}
ShellWh = TextEditInit( ShellWindowID, windowName ); /* true, it is the Shell Window */
SetShellName( ShellWh, NULL );
MyShell = (ShellWindRec **) (**(ShellWh)).thing;
(**ShellWh).MenuState.Frevert = 0; /* no revert for the Shell window */
(**ShellWh).MenuState.Fsave = 0; /* no save for the Shell window - use save as... */
if( GetWHandler( FrontWindow() ) != ShellWh )
TeWActivate( FALSE, ShellWh ); /* de-highlight scrollbars... */
(**ShellWh).whFrontOnly = true; /* true if idle only when in front */
( **MyShell ).shellID = numShells;
( **MyShell ).firstCmdNum = 0;
( **MyShell ).lastCmdNum = 0;
( **MyShell ).currCmdNum = 0;
GetPWDInfo( &((**MyShell).pwdVRefNum), &((**MyShell).pwdDirID),
&((**MyShell).parDirID) );
InitShellVars( ShellWh );
InitProcMgr( ShellWh );
DoShellPrompt( ShellWh );
printDemoMess();
/*
* Execute the '.profile' file
*/
if( (numShells == 1 && ShellPrefs.useProfileStartup)
|| (numShells > 1 && ShellPrefs.useProfileNewShell) )
{
i = ScanDir( ".profile", &((**MyShell).pwdVRefNum),
&((**MyShell).pwdDirID), &((**MyShell).parDirID) );
if( i == pathIsFile )
{
CommandString( ".profile\n", ShellWh );
}
}
numShells++;
SetWD( (**MyShell).pwdVRefNum, (**MyShell).pwdDirID );
}
/*******************************************************************/
void ResetShellPWD( WHandle ShellWh )
{
ShellWindRec **MyShell;
if( ShellWh && ((**ShellWh).WindAppType == ShellWindowID ))
{
MyShell = (ShellWindRec **) (**(ShellWh)).thing;
SetWD( (**MyShell).pwdVRefNum, (**MyShell).pwdDirID );
SetShellName( ShellWh, NULL );
}
}
/*******************************************************************/
void ShellAddStr( s, ShellWh )
char *s;
WHandle ShellWh; /* Window Handler Handle */
{
int32 len;
PeditWindRec **MyPedit;
GrafPtr port;
int32 free;
PEHandle hPE;
free = FreeMem();
GetPort( &port);
if( ShellWh == NULL )
ShellWh = currShell;
if( ShellWh == NULL )
return;
MyPedit = (PeditWindRec **) ((**ShellWh).thing);
SetPort( (**ShellWh).whWind);
hPE = (**MyPedit).hPE;
/* will this insert overflow memory ? */
len = (int32) strlen( s );
if( free < len )
{
PESetSelect( 0L, len, hPE );
PEDelete( hPE );
}
PESetSelect( (**hPE).peLength, (**hPE).peLength, hPE );
PEInsert( s, len, hPE );
if( (**MyPedit).inInput )
{
(**MyPedit).inInput = FALSE;
AdjustVScroll( ShellWh, true );
(**MyPedit).inInput = TRUE;
}
else
AdjustVScroll( ShellWh, true );
/* have we grown past the maximum character specification ? */
if( (**hPE).peLength > ShellPrefs.ShellCharLimit )
{
int32 cutPoint;
cutPoint = (**hPE).peLength - (ShellPrefs.ShellCharLimit - 2048);
cutPoint = PEBol( cutPoint, hPE );
PESetSelect( 0L, cutPoint, hPE );
PEDelete( hPE );
PESetSelect( (**hPE).peLength, (**hPE).peLength, hPE );
}
SetPort( port );
}